import axios from 'axios'
import type { ApiResult, JobTriggerStatusVO } from '@/types/job'

// 创建axios实例
const api = axios.create({
  // baseURL: 'http://localhost:8080/job-scheduler',
  baseURL: 'http://106.12.75.108:18002/job-scheduler',
  timeout: 10000,
})

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 任务信息相关接口
export const jobInfoApi = {
  // 分页查询任务信息
  queryJobInfoPage: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/job/info/page', params)
  },

  // 获取任务详情
  getJobInfoDetail: (jobId: number): Promise<ApiResult<any>> => {
    return api.get(`/api/job/info/detail/${jobId}`)
  },

  // 触发任务
  triggerJob: (jobId: number): Promise<ApiResult<string>> => {
    return api.post(`/api/job/info/trigger/${jobId}`)
  }
}

// 线程池监控相关接口
export const threadPoolMonitorApi = {
  // 分页查询监控数据
  queryMonitorData: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/thread-pool-monitor/query', params)
  },

  // 查询监控统计信息
  queryMonitorStatistics: (registerKey: string, startTime: string, endTime: string): Promise<ApiResult<any>> => {
    return api.get(`/api/thread-pool-monitor/statistics/${registerKey}`, {
      params: { startTime, endTime }
    })
  },

  // 查询执行器统计信息
  queryExecutorStatistics: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/thread-pool-monitor/statistics', params)
  },

  // 获取监控数据概览
  getMonitorOverview: (): Promise<ApiResult<any>> => {
    return api.get('/api/thread-pool-monitor/overview')
  },

  // 查询最新监控数据
  queryLatestMonitorData: (registerKey: string, limit: number = 10): Promise<ApiResult<any>> => {
    return api.get(`/api/thread-pool-monitor/latest/${registerKey}`, {
      params: { limit }
    })
  },

  // 查询所有最新监控数据
  queryAllLatestMonitorData: (): Promise<ApiResult<any>> => {
    return api.get('/api/thread-pool-monitor/latest/all')
  },

  // 获取执行器列表
  getExecutorList: (): Promise<ApiResult<string[]>> => {
    return api.get('/api/thread-pool-monitor/executors')
  },

  // 获取应用名称列表
  getAppList: (): Promise<ApiResult<string[]>> => {
    return api.get('/api/thread-pool-monitor/apps')
  },

  // 清理历史监控数据
  cleanHistoryData: (days: number = 30): Promise<ApiResult<void>> => {
    return api.delete('/api/thread-pool-monitor/clean', {
      params: { days }
    })
  }
}

// 任务日志相关接口
export const jobLogApi = {
  // 分页查询任务日志
  queryJobLogPage: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/job/log/page', params)
  },

  // 获取任务日志详情
  getJobLogDetail: (jobId: number): Promise<ApiResult<any>> => {
    return api.get(`/api/job/log/detail/${jobId}`)
  },

  // 停止任务
  stopJob: (jobId: number): Promise<ApiResult<any>> => {
    return api.post(`/api/job/log/stop/${jobId}`)
  },

  // 查询任务每日执行统计
  queryJobDailyExecution: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/job/daily-execution', params)
  }
}

// 执行器运行情况API
export const executorRuntimeApi = {
  // 分页查询执行器运行情况
  queryExecutorRuntime: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/executor-runtime/query', params)
  },
  // 查询指定执行器运行情况
  getExecutorRuntime: (registerKey: string): Promise<ApiResult<any>> => {
    return api.get(`/api/executor-runtime/${registerKey}`)
  },
  // 获取执行器资源使用情况
  getResourceUsage: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/resource-usage')
  },
  // 获取执行器运行情况概览
  getRuntimeOverview: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/overview')
  },
  // 查询在线执行器列表
  getOnlineExecutors: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/online')
  },
  // 查询离线执行器列表
  getOfflineExecutors: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/offline')
  },
  // 获取执行器健康状态统计
  getHealthStatistics: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/health-statistics')
  },
  // 查询所有执行器运行情况
  getAllExecutorRuntime: (): Promise<ApiResult<any>> => {
    return api.get('/api/executor-runtime/all')
  },
  // 清理离线执行器状态
  cleanOfflineExecutors: (timeoutMinutes?: number): Promise<ApiResult<void>> => {
    return api.delete('/api/executor-runtime/clean-offline', { params: { timeoutMinutes: timeoutMinutes || 30 } })
  }
}

// 环境监控API
export const environmentMonitorApi = {
  // 分页查询环境监控数据
  queryEnvironmentMonitorPage: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/environment-monitor/page', params)
  },
  // 查询执行器统计信息
  queryEnvironmentMonitorStatistics: (params?: any): Promise<ApiResult<any>> => {
    return api.get('/api/environment-monitor/statistics', { params })
  },
  // 查询最新的环境监控数据
  queryLatestEnvironmentMonitor: (): Promise<ApiResult<any>> => {
    return api.get('/api/environment-monitor/latest')
  },
  // 查询指定执行器的最新环境监控数据
  queryLatestEnvironmentMonitorByRegisterKey: (registerKey: string): Promise<ApiResult<any>> => {
    return api.get(`/api/environment-monitor/latest/${registerKey}`)
  },
  // 查询执行器的环境监控历史数据
  queryEnvironmentMonitorHistory: (params: any): Promise<ApiResult<any>> => {
    return api.post('/api/environment-monitor/history', params)
  },
  // 获取所有执行器列表
  queryAllExecutors: (): Promise<ApiResult<any>> => {
    return api.get('/api/environment-monitor/executors')
  },
  // 清理历史数据
  cleanHistoryData: (days?: number): Promise<ApiResult<string>> => {
    return api.post('/api/environment-monitor/clean', { params: { days: days || 30 } })
  }
}

// 仪表盘API
export const dashboardApi = {
  // 获取首页概览数据
  getDashboardOverview: (): Promise<ApiResult<any>> => {
    return api.get('/api/dashboard/overview')
  },

  // 获取所有执行器列表
  getAllExecutors: (): Promise<ApiResult<any>> => {
    return api.get('/api/dashboard/executors')
  },

  // 获取执行器详情数据
  getExecutorDetail: (registerKey: string): Promise<ApiResult<any>> => {
    return api.get(`/api/dashboard/executor/${registerKey}`)
  },

  // 获取执行器24小时运行趋势
  getExecutorTrend: (registerKey: string, startTime?: string, endTime?: string): Promise<ApiResult<any>> => {
    return api.get(`/api/dashboard/executor/${registerKey}/trend`, {
      params: { startTime, endTime }
    })
  },

  // 获取在线执行器趋势数据
  getOnlineExecutorsTrend: (date?: string): Promise<ApiResult<any>> => {
    return api.get('/api/dashboard/online-executors/trend', {
      params: { date }
    })
  }
}

// 任务触发器管理API
export const jobTriggerApi = {
  // 开启定时任务触发处理器
  startJobTrigger: (): Promise<ApiResult<string>> => {
    return api.post('/api/job-trigger/start')
  },

  // 关闭定时任务触发处理器
  stopJobTrigger: (): Promise<ApiResult<string>> => {
    return api.post('/api/job-trigger/stop')
  },

  // 获取任务触发器状态
  getJobTriggerStatus: (): Promise<ApiResult<JobTriggerStatusVO>> => {
    return api.get('/api/job-trigger/status')
  }
}

export default api
